"use strict";
cc._RF.push(module, 'e6dbf+TDB1GV5h01wKup7rx', 'node_component');
// scripts/engines/libs/node_component.ts

"use strict";
/**
 * 节点组件相关方法
 * */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var loadSceneInterface = /** @class */ (function () {
    function loadSceneInterface(name) {
        this.totalCount = 0;
        this.completed = 0;
        this.name = name;
    }
    return loadSceneInterface;
}());
var mapLoadInfo = /** @class */ (function () {
    function mapLoadInfo() {
    }
    return mapLoadInfo;
}());
var loadScenesArray = /** @class */ (function () {
    function loadScenesArray() {
        this.map = new mapLoadInfo();
        this.m_overEd = false;
    }
    loadScenesArray.prototype.initStart = function () {
    };
    loadScenesArray.prototype.initIng = function () {
        for (var key in this.map) {
            if (this.map[key].totalCount == 0) {
                return true;
            }
        }
        return false;
    };
    /**
     * @param {String} name
     * @param {loadSceneInterface} loadSceneInfo
     * */
    loadScenesArray.prototype.push = function (name, loadSceneInfo) {
        this.map[name] = loadSceneInfo;
    };
    loadScenesArray.prototype.getAllTotalCount = function () {
        var count = 0;
        for (var name in this.map) {
            count += this.map[name].totalCount;
        }
        return count;
    };
    loadScenesArray.prototype.getAllCompletedCount = function () {
        var count = 0;
        for (var name in this.map) {
            count += this.map[name].completed;
        }
        return count;
    };
    loadScenesArray.prototype.setCompleted = function (name, count) {
        this.map[name].completed = count;
    };
    loadScenesArray.prototype.setTotalCount = function (name, count) {
        this.map[name].totalCount = count;
    };
    loadScenesArray.prototype.setLoaded = function (name) {
        this.map[name].completed = this.map[name].totalCount;
    };
    /**
     * @return {Boolean}
     * */
    loadScenesArray.prototype.isLoadOver = function () {
        //防止多次触发
        if (this.m_overEd)
            return false;
        //只触发一次
        this.m_overEd = this.getAllTotalCount() == this.getAllCompletedCount();
        return this.m_overEd;
    };
    return loadScenesArray;
}());
/**
 * 节点和组件方法集合
 * */
var node_component = /** @class */ (function () {
    function node_component() {
    }
    /***
     * @function 下一帧执行
     * @param {cc.Component} com 组件
     * @param {Function} func 函数
     * */
    node_component.nextTickRun = function (com, func) {
        com.scheduleOnce(func, 0);
    };
    /**
     * @function 指定多少帧后执行
     * @param {cc.Component} com 组件
     * @param {Function} func 函数
     * @param {Number} num 帧数
     * */
    node_component.numFpsAfterRun = function (com, func, num) {
        var _this = this;
        var i = num;
        var a = function () {
            _this.nextTickRun(com, function () {
                if (--i <= 0) {
                    func();
                }
                else {
                    a();
                }
            });
        };
        a();
    };
    /**
     * @function 合并加载场景
     * */
    node_component.mergePreLoadScenes = function (sceneNames, onProgress, onLoaded) {
        var loadArray = new loadScenesArray();
        loadArray.initStart();
        var _loop_1 = function (sceneName) {
            loadArray.push(sceneName, new loadSceneInterface(sceneName));
            //
            cc.director.preloadScene(sceneName, function (completedCount, totalCount, item) {
                loadArray.setCompleted(sceneName, completedCount);
                loadArray.setTotalCount(sceneName, totalCount);
                if (loadArray.initIng())
                    return;
                onProgress && onProgress(loadArray.getAllCompletedCount(), loadArray.getAllTotalCount(), item);
            }, function () {
                loadArray.setLoaded(sceneName);
                if (loadArray.initIng())
                    return;
                if (loadArray.isLoadOver()) {
                    onLoaded && onLoaded();
                }
            });
        };
        for (var _i = 0, sceneNames_1 = sceneNames; _i < sceneNames_1.length; _i++) {
            var sceneName = sceneNames_1[_i];
            _loop_1(sceneName);
        }
        //
    };
    /**
      * 加载resources 图片
      * @param url
      * @param spriteComOrSpriteNode
      */
    node_component.loadResourcesPicture = function (url, spriteComOrSpriteNode, bundle) {
        if (bundle === void 0) { bundle = cc.resources; }
        return __awaiter(this, void 0, Promise, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, new Promise(function (resolve, reject) {
                        bundle.load(url, cc.SpriteFrame, function (error, spriteFrame) {
                            if (error) {
                                console.error('图片不存在', url);
                                // console.error(error);
                                reject();
                                return;
                            }
                            if (spriteComOrSpriteNode instanceof cc.Node) {
                                var sprite = spriteComOrSpriteNode.getComponent(cc.Sprite) || spriteComOrSpriteNode.addComponent(cc.Sprite);
                                sprite.spriteFrame = spriteFrame;
                            }
                            else if (spriteComOrSpriteNode instanceof cc.Sprite) {
                                spriteComOrSpriteNode.spriteFrame = spriteFrame;
                            }
                            else {
                                throw '加载非图片不能使用此方法';
                            }
                            resolve(spriteFrame);
                        });
                    })];
            });
        });
    };
    /**
     * 加载一张 图片
     * @param url
     */
    node_component.loadSpriteFrame = function (url, bundle) {
        if (bundle === void 0) { bundle = cc.resources; }
        return __awaiter(this, void 0, Promise, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, new Promise(function (resolve) {
                        bundle.load(url, cc.SpriteFrame, function (error, spriteFrame) {
                            if (error) {
                                console.error('图片不存在', url);
                                console.error(error);
                                return;
                            }
                            resolve(spriteFrame);
                        });
                    })];
            });
        });
    };
    /**
     * 获取相对于指定父节点的位置
     * */
    node_component.getPosOfParentNode = function (srcNode, parentNode) {
        var curNode = srcNode;
        var curPos = cc.v2(0, 0);
        while (curNode != parentNode) {
            curPos = curPos.add(curNode.getPosition());
            curNode = curNode.parent;
        }
        return curPos;
    };
    /**
     * 获取指定帧数时的耗时
     * */
    node_component.getTimeOfFps = function (fpsCount) {
        return 1 / 60 /*cc.game.getFrameRate()*/ * fpsCount;
    };
    return node_component;
}());
exports.default = node_component;

cc._RF.pop();